package com.kafka_demo.kafka;

import com.kafka_demo.kafka.listener.KafkaProducerListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

/**
 * kafka生产者
 *
 * @author : z_sz
 * @date : 2020-09-03 10:49
 **/
@Component
public class KafkaProducerManager {

    /**
     * 消息主体
     */
    private final String topic = "KAFKA_TEST";

    /**
     * kafkaTemplate模板类
     */
    @Autowired
    private KafkaTemplate<Object, Object> kafkaTemplate;

    /**
     * kafka推送回调监听器
     */
    @Autowired
    private KafkaProducerListener kafkaProducerListener;

    /**
     * 消息发送
     *
     * @param message 消息内容
     */
    public void send(String message) {
        kafkaTemplate.setProducerListener(kafkaProducerListener);
        kafkaTemplate.send(topic, message);
    }

}
